//------------------------------------------------------------------------------
//  此代码版权（除特别声明或在RRQMCore.XREF命名空间的代码）归作者本人若汝棋茗所有
//  源代码使用协议遵循本仓库的开源协议及附加协议，若本仓库没有设置，则按MIT开源协议授权
//  CSDN博客：https://blog.csdn.net/qq_40374647
//  哔哩哔哩视频：https://space.bilibili.com/94253567
//  Gitee源代码仓库：https://gitee.com/RRQM_Home
//  Github源代码仓库：https://github.com/RRQM
//  交流QQ群：234762506
//  感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;

namespace ShowDocGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.ReadKey();
            Run();
            Console.ReadKey();
        }

        private static void Run()
        {

            Assembly assembly =Assembly.LoadFrom(@"E:\CodeOpen\RRQMSocket\RRQMSocket\bin\Debug\net45\RRQMSocket.dll");
            XmlDocument xml = new XmlDocument();
            xml.Load(@"E:\CodeOpen\RRQMSocket\RRQMSocket\bin\Debug\net45\RRQMSocket.xml");

            XmlNode members = xml.SelectSingleNode("doc/members");
            Type[] types = assembly.GetTypes();

            foreach (Type type in types)
            {
                if (type.IsPublic)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendLine($"# {type.Name}");
                    stringBuilder.AppendLine($"命名空间：RRQMSocket");
                    stringBuilder.AppendLine($"程序集：{assembly.GetName().Name}.dll");
                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine($"##### 简要描述");

                    foreach (XmlNode item in members.ChildNodes)
                    {
                        if (item.Attributes["name"].Value == "T:" + type.FullName)
                        {
                            stringBuilder.AppendLine(item.SelectSingleNode("summary").InnerText.Trim());
                            break;
                        }
                    }
                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine($"##### 属性");


                    PropertyInfo[] propertyInfoes = type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);

                    foreach (var propertyInfo in propertyInfoes)
                    {
                        string note = string.Empty;
                        foreach (XmlNode item in members.ChildNodes)
                        {
                            if (item.Attributes["name"].Value == $"P:{propertyInfo.DeclaringType.FullName}.{propertyInfo.Name}")
                            {
                                note = item.SelectSingleNode("summary").InnerText.Trim();
                                break;
                            }
                        }
                        stringBuilder.AppendLine($"- `{propertyInfo.Name}`:{note}");
                    }
                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine($"##### 事件");
                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine($"|事件名| 触发 |");
                    stringBuilder.AppendLine($"|--|--|");

                    EventInfo[] eventInfoes = type.GetEvents(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);

                    foreach (var eventInfo in eventInfoes)
                    {
                        string note = string.Empty;
                        foreach (XmlNode item in members.ChildNodes)
                        {
                            if (item.Attributes["name"].Value == $"E:{eventInfo.DeclaringType.FullName}.{eventInfo.Name}")
                            {
                                note = item.SelectSingleNode("summary").InnerText.Trim();
                                break;
                            }
                        }
                        stringBuilder.AppendLine($"| {eventInfo.Name} | {note} |");
                    }


                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine($"##### 方法");
                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine($"|方法名| 功能 |");
                    stringBuilder.AppendLine($"|--|--|");
                    MethodInfo[] methodInfoes = type.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
                    foreach (var method in methodInfoes)
                    {
                        string note = string.Empty;
                        foreach (XmlNode item in members.ChildNodes)
                        {
                            if (item.Attributes["name"].Value == $"M:{method.DeclaringType.FullName}.{method.Name}")
                            {
                                note = item.SelectSingleNode("summary").InnerText.Trim();
                                break;
                            }
                        }
                        stringBuilder.Append($"| {method.Name}");
                        stringBuilder.Append($"(");
                        int i = 0;
                        foreach (var item in method.GetParameters())
                        {
                            stringBuilder.Append(item.Name);
                            i++;
                            if (i < method.GetParameters().Length)
                            {
                                stringBuilder.Append(",");
                            }
                        }

                        stringBuilder.Append($")");
                        stringBuilder.AppendLine($" | {note} |");
                    }

                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine("##### 使用 ");
                    stringBuilder.AppendLine();
                    stringBuilder.AppendLine("具体的创建步骤详见[C# 创建高并发、高性能TCP通信框架](https://blog.csdn.net/qq_40374647/article/details/110679663?spm=1001.2014.3001.5501)");
                    stringBuilder.AppendLine();


                    Dictionary<string, string> keyValues = new Dictionary<string, string>();
                    keyValues.Add("api_key", "af39876e73792e2de903cb7616dd855b1575541966");
                    keyValues.Add("api_token", "5c34ba65e65af9fbfebb34f9302f506873623225");
                    keyValues.Add("cat_name", "Test");
                    keyValues.Add("page_title", $"{type.Name}");
                    keyValues.Add("page_content", stringBuilder.ToString());
                    keyValues.Add("s_number", "99");

                    Post("https://www.showdoc.cc/server/api/item/updateByApi", keyValues);
                    Thread.Sleep(100);
                }

            }

        }

        public static void Post(string url, Dictionary<string, string> dic)
        {
            string result = "";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            #region 添加Post 参数
            StringBuilder builder = new StringBuilder();
            int i = 0;
            foreach (var item in dic)
            {
                if (i > 0)
                    builder.Append("&");
                builder.AppendFormat("{0}={1}", item.Key, item.Value);
                i++;
            }
            byte[] data = Encoding.UTF8.GetBytes(builder.ToString());
            req.ContentLength = data.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
            }
            #endregion
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            Stream stream = resp.GetResponseStream();
            //获取响应内容
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                result = reader.ReadToEnd();
            }
            if (result.Contains("error_code\":0"))
            {
                Console.WriteLine(dic["page_title"] + "-----成功");
            }
            else
            {
                Console.WriteLine(dic["page_title"] + "-----失败");
            }
        }
    }
}
